<!--
 * @Description: 
 * @Author: Sun
 * @Date: 2019-06-12 21:31:04
 * @LastEditors: Sun
 * @LastEditTime: 2019-06-30 11:31:45
 -->
<template>
  <div class="progress-bar" ref="progressBar" @click="progressClick">
		<div class="bar-inner">
			<!-- 走过的位置 -->
			<div class="progress" ref="progress"></div>
			<!-- 当前位置和小圆圈 -->
			<div class="progress-btn-wrapper" ref="progressBtn"
				@touchstart.prevent="progressTouchStart"
				@touchmove.prevent="progressTouchMove"
				@touchend="progressTouchEnd">
				<div class="progress-btn"></div>
			</div>
		</div>	
  </div>
</template>

<script>
import {prefixStyle} from 'common/js/dom'
const progressBtnWidth = 16
const transform = prefixStyle('transform')
  export default {
		name: 'progressbar',
    props: {
      percent: {//进度条%
        type: Number,
        default: 0
      }
    },
    created() {
      this.touch = {}
    },
    methods: {
			// 小球手指3事件
      progressTouchStart(e) { //按下
        this.touch.initiated = true//标志位
        this.touch.startX = e.touches[0].pageX
        this.touch.left = this.$refs.progress.clientWidth
      },
      progressTouchMove(e) {//移动
        if (!this.touch.initiated) {
          return
        }
				//手指纵向是我偏移量
        const deltaX = e.touches[0].pageX - this.touch.startX
        //手指可能大于总长度 要加以限制
				const offsetWidth = Math.min(
						this.$refs.progressBar.clientWidth - progressBtnWidth, 
						Math.max(0, this.touch.left + deltaX)
				)
        this._offset(offsetWidth)
      },
      progressTouchEnd() {//抬起
        this.touch.initiated = false
				//结束派发事件
        this._triggerPercent()
      },
			//获取小球偏移量
      progressClick(e) {
				// getBoundingClientRect用于获取某个元素相对于视窗的位置集合。集合中有top, right, bottom, left等属性。
				// https://www.cnblogs.com/Songyc/p/4458570.html  一个固定的宽度
        const rect = this.$refs.progressBar.getBoundingClientRect()
        const offsetWidth = e.pageX - rect.left//文档坐标
				console.log("rect",rect)
				console.log("rect",e.pageX)
				console.log("offsetWidth",offsetWidth)
        this._offset(offsetWidth)
        // 这里当我们点击 progressBtn 的时候，e.offsetX 获取不对
        // this._offset(e.offsetX)
        this._triggerPercent()
      },
      _triggerPercent() { //实时更新时间
        const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth //真实滚动条宽度
        const percent = this.$refs.progress.clientWidth / barWidth
				console.log("trigg",percent)
        this.$emit('percentChange', percent)
      },
      _offset(offsetWidth) {
				// 黄色的进度条  小球的偏移
        this.$refs.progress.style.width = `${offsetWidth}px`
        this.$refs.progressBtn.style[transform] = `translate3d(${offsetWidth}px,0,0)`
      }
    },
    watch: { //监听滚动条的变化
      percent(newPercent) {
        if (newPercent >= 0 && !this.touch.initiated) {
					// 滚动条减去-小圆球的宽度
          const barWidth = this.$refs.progressBar.clientWidth - progressBtnWidth
          const offsetWidth = newPercent * barWidth
          this._offset(offsetWidth)
        }
      }
    }
  }
</script>

<style lang="scss" scoped >
	.progress-bar{
		height: 30px;
		.bar-inner{
			position: relative;
			top: 13px;
			height: 4px;
			background: rgba(0,0,0,.3);
			.progress{
				position: absolute;
				height: 100%;
				background: $color-theme;
			}
			.progress-btn-wrapper{
				position: absolute;
				left: -8px;
				top: -13px;
				width: 30px;
				height: 30px;
				.progress-btn{
					position: relative;
					top: 7px;
					left: 7px;
					box-sizing: border-box;
					width: 16px;
					height: 16px;
					border: 3px solid $color-text;
					border-radius: 50%;
					background: $color-theme;
				}
			}
		}
	}
</style>